package com.lyc.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;

/**
 * @author Administrator
 * @date 2024/4/27
 * @project hushan-iot-platform
 * @package com.lyc.service
 */
public class FileUtils {
    public static void removeLine(String lineText, String filePath) throws IOException {
        BufferedReader bufferedReader = IoUtil.getReader(FileUtil.getInputStream(filePath), Charset.defaultCharset());
        String tempFile = filePath + ".tmp";
        PrintWriter printWriter = new PrintWriter(new FileWriter(FileUtil.file(tempFile)));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (!line.trim().equals(lineText)) {
                printWriter.println(line);
                printWriter.flush();
            }
        }

        printWriter.close();
        bufferedReader.close();
        FileUtil.move(FileUtil.file(tempFile), FileUtil.file(filePath), true);
    }
}
